﻿function AzureOnlineBlobContainerCreateDetailsView(context) {

    var dispatcher = $({});
    var name = ko.observable('');
    var triedToSubmit = ko.observable(false);

    var validName = ko.computed(function () {
        if (!triedToSubmit()) return true;
        if (showNameRequired()) return false;
        if (showMustContainAlphaNumericDashesOnly()) return false;
        if (showFirstLastDash()) return false;
        if (showConsecutiveDash()) return false;
        if (showLowerCase()) return false;
        if (showLength()) return false;
        return true;
    });

    var showNameRequired = ko.computed(function () {
        if (!triedToSubmit()) return false;
        var n = name().trim();
        if (utils.isNullOrWhiteSpace(n)) return true;
        return false;
    });

    var showMustContainAlphaNumericDashesOnly = ko.computed(function () {
        if (!triedToSubmit()) return false;
        var n = name().trim();
        if (utils.isNullOrWhiteSpace(n)) return false;
        for (var i = 0; i < n.length; i++) {
            var ch = n.charAt(i);
            if (!utils.isAlpha(ch) && !utils.isDigit(ch) && ch != '-') {
                return true;
            }
        }
        return false;
    });

    var showFirstLastDash = ko.computed(function () {
        if (!triedToSubmit()) return false;
        var n = name().trim();
        if (utils.isNullOrWhiteSpace(n)) return false;
        var ch = n.charAt(0);
        if (ch == '-') {
            return true;
        }
        ch = n.charAt(n.length - 1);
        if (ch == '-') {
            return true;
        }
        return false;
    });

    var showConsecutiveDash = ko.computed(function () {
        if (!triedToSubmit()) return false;
        var n = name().trim();
        if (utils.isNullOrWhiteSpace(n)) return false;
        return n.indexOf('--') >= 0;
    });

    var showLowerCase = ko.computed(function () {
        if (!triedToSubmit()) return false;
        var n = name().trim();
        if (utils.isNullOrWhiteSpace(n)) return false;
        for (var i = 0; i < n.length; i++) {
            var ch = n.charAt(i);
            if (ch >= 'A' && ch <= 'Z') {
                return true;
            }
        }
        return false;
    });

    var showLength = ko.computed(function () {
        if (!triedToSubmit()) return false;
        var n = name().trim();
        if (utils.isNullOrWhiteSpace(n)) return false;
        if (n.length < 3) return true;
        if (n.length > 63) return true;
        return false;
    });

    var spaRuntime = context.spaRuntime;

    var blobDetailsRoute = spaRuntime.findRouteByName('blobContainerDetails');

    function reset() {
        triedToSubmit(false);
        name('');
    }

    function validate() {
        triedToSubmit(true);
        return validName();
    }

    function create() {
        if (validate()) {
            var api = context.api();
            var container = name();
            api.blob.createContainer({
                container: container,
                success: function () {
                    var newItem = new AzureOnlineBlobContainer();
                    newItem.name(container);
                    context.blobs.addNewContainers(newItem);
                    reset();
                    dispatcher.trigger('success');
                    window.location.hash = blobDetailsRoute.formatHash({
                        blobName: container
                    });
                }
            });
        }
    }

    this.name = name;
    this.validName = validName;
    this.hasErrors = ko.computed(function () {
        return !validName();
    });
    this.showNameRequired = showNameRequired;
    this.showMustContainAlphaNumericDashesOnly = showMustContainAlphaNumericDashesOnly;
    this.showFirstLastDash = showFirstLastDash;
    this.showConsecutiveDash = showConsecutiveDash;
    this.showLowerCase = showLowerCase;
    this.showLength = showLength;

    this.reset = reset;
    this.create = create;

    this.addOnSuccess = function (handler) {
        dispatcher.bind('success', handler);
    };
}